package org.forum.controller.interceptor;


import java.util.Map;

import org.forum.controller.Interface.PAGES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 用于UserController
 * 检验当前用户是否已经登陆,仅有两个action不拦截
 * @author One
 *
 */
public class AuthorityInterceptor extends AbstractInterceptor{

	Logger logger = LoggerFactory.getLogger(AuthorityInterceptor.class);
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();
		Map session =  actionContext.getSession();
		if(logger.isDebugEnabled()){
			logger.debug("执行action的方法:" +invocation.getAction().toString()+"." + invocation.getProxy().getMethod());
		}
		//判断调用的方法是否是login
		if(invocation.getProxy().getMethod() == "login" ){
			return invocation.invoke();
		}
		//判断调用方法是否是register
		if(invocation.getProxy().getMethod() == "register"){
			return invocation.invoke();
		}
		//判断调用方法是否是gotoLoginPage
		if(invocation.getProxy().getMethod() == "gotoLoginPage"){
			return invocation.invoke();
		}
		//判断调用方法是否是gotoRegisterPage
		if(invocation.getProxy().getMethod() == "goResiterPage"){
			return invocation.invoke();
		}
		//判断调用方法是否是gotoHomePage
		if(invocation.getProxy().getMethod() == "gotoHomePage"){
			return invocation.invoke();
		}
		//判断调用方法是否是generateLoginSecurityCode
		if(invocation.getProxy().getMethod() == "generateLoginSecurityCode"){
			return invocation.invoke();
		}
		//判断调用方法是否是generatedRegisterSecurityCode
		if(invocation.getProxy().getMethod() == "generateRegisterSecurityCode"){
			return invocation.invoke();
		}
		
		//判断用户会话是否存在User对象
		if(null == session.get("user") ){
			actionContext.put("tip", "您还没有登陆");
			return PAGES.LOGINPAGE;
		}
		return invocation.invoke();
	}

}
